﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Model;
using Kaidou.Persister;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

public partial class master_master_union_search : System.Web.UI.Page
{
    private int pageSize = int.Parse(Resources.Resource.pagenumber);

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];

        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }
        else if ( loginModel.isKumiaiin() )
        {
            Response.Redirect("../menu.aspx");
        }

        string area = Resources.Resource.area;
        string[] areaArray = area.Split(',');
        for (int i = 0; i < areaArray.Length; i++)
        {
            ListItem item = new ListItem();
            item.Value = (i + 1) + "";
            item.Text = areaArray[i];
            office_pref_code.Items.Insert(i + 1, item);
        }


        if (!IsPostBack)
        {
            table.Visible = false;
            none.Visible = false;
        }

        if (Session["back"] != null && (int.Parse(Session["back"].ToString()) == 1))
        {
          
            MstUnion M = (MstUnion)Session["searchMst"];
            if (M.Id.UnionCode != null)
            {
                union_code.Text = M.Id.UnionCode;
            }

            if (M.OfficeName != null)
            {
                office_name.Text = M.OfficeName;
            }

            if (M.OfficeKana != null)
            {
                office_kana.Text = M.OfficeKana;
            }
            if (M.OfficePrefCode>0)
            {
                office_pref_code.Text = M.OfficePrefCode.ToString();
            }
           

            if (M.TraderRegNo != null && M.TraderRegNo.Trim().Length > 0)
            {
                trader_reg_no1.Text = M.TraderRegNo1;
                trader_reg_no2.Text = M.TraderRegNo2;
            }

            page(int.Parse(Session["pageNo"].ToString()), true);

            Session.Remove("back");
        }
        
        

    }

    private void page(int no,bool back)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];
        //カプセル化オブジェクト
        MstUnion M = new MstUnion();
        if (!back)
        {
            MstUnionId MID = new MstUnionId();
            MID.UnionCode = union_code.Text.Trim().Length > 0 ? union_code.Text.Trim() : null;
            M.Id = MID;
            M.OfficeKana = office_kana.Text.Trim().Length > 0 ? office_kana.Text.Trim() : null;
            M.OfficeName = office_name.Text.Trim().Length > 0 ? office_name.Text.Trim() : null;
            M.OfficePrefCode = office_pref_code.Text.Trim().Length > 0 ? short.Parse(office_pref_code.Text) : (short)0;
            M.TraderRegNo = (trader_reg_no1.Text.Trim().Length > 0 || trader_reg_no2.Text.Trim().Length > 0) ? (trader_reg_no1.Text + trader_reg_no2.Text) : null;
            M.TraderRegNo1 = trader_reg_no1.Text.Trim().Length > 0 ? trader_reg_no1.Text : null;
            M.TraderRegNo2 = trader_reg_no2.Text.Trim().Length > 0 ? trader_reg_no2.Text : null;
        }
        else
        {
            M = (MstUnion)Session["searchMst"];
            
        }

        Session["searchMst"] = M;
        Session["pageNo"] = no;

        MST_UnionPersister dao = new MST_UnionPersister();
        if (no <= 0) no = 1;
        Pager pager = dao.search(M, no, pageSize);
        
        //if (no > pager.Pages) no = pager.Pages;
        //pager = dao.search(M, no, pageSize);
        pageNo.Value = no.ToString();
        pages.Text = "全" + pager.Total + "件";
        ArrayList list = pager.List;

        if(list.Count==0){
            table.Visible = false;
            none.Visible = true;
        }else{
            none.Visible = false;
            table.Visible = true;
            //datatable.Rows.Clear();
            if (no == 1)
            {
                prebutton.Visible = false;
            }
            else
            {
                prebutton.Visible = true;
                prebutton.Text = "≪ 前の" + pageSize + "件";

            }

            if (no == pager.Pages)
            {
                nextbutton.Visible = false;
                
            }
            else
            {
                nextbutton.Visible = true;
                nextbutton.Text = " 次の" + pageSize + "件 ≫";
            }

            string area = Resources.Resource.area;
            string[] areaArray = area.Split(',');
            StringBuilder sb = new StringBuilder();
            foreach (object o in list)
            {
                MstUnion mu = (MstUnion)o;
                TableRow row = new TableRow();

                TableCell td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Center;
                td.Text = mu.Id.UnionCode;
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.Text = mu.OfficeName;
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.Text = areaArray[mu.OfficePrefCode-1];
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.Text = mu.OfficeCity;
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Center;
                if (mu.TraderRegNo != null && mu.TraderRegNo.Trim().Length > 0)
                {
                    td.Text = mu.TraderRegNo.Substring(0, 2) + "-" + mu.TraderRegNo.Substring(2, 5);
                }
                
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Center;
                if (!loginModel.isOperator())
                {
                    td.Text = "<a href='master_union_view.aspx?unioncode=" + mu.Id.UnionCode + "'>表示</a>";
                }
                else
                {

                    td.Text = "<a href='master_union_detail.aspx?unioncode=" + mu.Id.UnionCode + "'>表示</a>";
                }
                row.Cells.Add(td);

                datatable.Rows.Add(row);
            }

        }
    }

    protected void pre(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchMst");
        Session.Remove("pageNo");
        page(int.Parse(pageNo.Value)-1,false);
    }

    protected void next(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchMst");
        Session.Remove("pageNo");
        page(int.Parse(pageNo.Value)+1,false);
    }

    protected void search(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchMst");
        Session.Remove("pageNo");
        page(1, false);
        

    }

  
}
